<?php
#require_once('site_config.php');
class Session{
	
	#protected $session_name = 'SESS';
	public $session_name = 'SESS';
	public $session_path = '';
	// each site may custom the session handling a bit
  //   e.g. file base / mysql / memcached
  // this function is called by setup(), defined in CGIApplication.class.php
	function __construct($session_name=''){    
    global $site_global_config;

    $this->session_name = $session_name == ''?$site_global_config['SESSION_NAME']:$session_name;
    $session_type = $site_global_config['SESSION_TYPE'];
	  switch($session_type){
	  	case 'MYSQL':
	  	  #echo 'Using mysql to store session';
	      $this->handle_session_by_mysql();
	      break;
	    case 'MEMCACHE':
	  	  #echo 'Using memcache to store session';
	      $this->handle_session_by_memcache();
	      break;
	    default:
	      $this->session_path = $site_global_config['SESSION_PATH'];     
	      $this->handle_session_by_local_file();
	   }
	    
    #try {  	
      @session_start(); # support mysql / memcahced session?
    #} catch (Exception $e) {
    #  throw new CgiappErrorException(81, 'Unable to connect to db server. Error:'.$e->getMessage());
    #}	    
	   
	}
	
	function get($key, $default=''){
		$skey = $this->session_name.'_'.$key;
		
		return isset($_SESSION[$skey]) ? $_SESSION[$skey] : $default; 
	}
	
	function set($key, $value){
		$_SESSION[$this->session_name.'_'.$key] = $value;
		return $value; 
	}
	
	
	function handle_session_by_memcache(){
    ##$session_save_path = "tcp://1270.0.1:$port?persistent=1&weight=2&timeout=2&retry_interval=10,  ,tcp://$host:$port  ";
    #$session_save_path = "tcp://127.0.0.1:11211?persistent=1&weight=2&timeout=2&retry_interval=10";
    #ini_set('session.save_handler', 'memcache');
    #ini_set('session.save_path', $session_save_path);
    
    // session expire time
	  session_name($this->session_name);

    
    require_once('SessionHandler.Memcache.class.php');
    $session = new SessionHandler_Memcache();
    session_set_save_handler(array($session, 'open'),
                           array($session, 'close'),
                           array($session, 'read'),
                           array($session, 'write'),
                           array($session, 'destroy'),
                           array($session, 'gc'));


	}
	
	function handle_session_by_mysql(){
    // session expire time
	  session_name($this->session_name);
    #require_once('SessionHandler.Mysql.class.php');
    #$session = new SessionHandler_Mysql();
    require_once('SessionHandler.PDOMysql.class.php');
    $session = new SessionHandler_PDOMysql();
    session_set_save_handler(array($session, 'open'),
                           array($session, 'close'),
                           array($session, 'read'),
                           array($session, 'write'),
                           array($session, 'destroy'),
                           array($session, 'gc'));
	}

  function handle_session_by_local_file(){
  	
    session_name($this->session_name);
	session_save_path($this->session_path);	    		  
  }

 
} # end Class Session
?>